package com.neihe.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.neihe.entity.dto.user.CheckCodeDTO;
import com.neihe.entity.dto.user.LoginFormDTO;
import com.neihe.entity.dto.user.RegisterFormDTO;
import com.neihe.entity.dto.user.ResetPwdFormDTO;
import com.neihe.entity.vo.user.UseSpaceVO;
import com.neihe.entity.vo.user.UserInfoVO;
import com.neihe.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 用户模块
 *
 * @author neihe
 * @since 2025-01-04
 */
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 发送邮箱验证码
     *
     * @param email   邮箱
     * @param request 请求对象
     */
    @GetMapping("/api/user/public/email/send-code")
    public void sendCode(@RequestParam String email, HttpServletRequest request) {
        userService.sendCode(email, request);
    }

    /**
     * 检查邮箱验证码
     *
     * @param requestParam 请求参数
     */
    @PostMapping("/api/user/public/email/check-code")
    public void checkCode(@Valid @RequestBody CheckCodeDTO requestParam) {
        userService.checkCode(requestParam);
    }

    /**
     * 用户注册
     *
     * @param requestParam 注册表单请求参数
     */
    @PostMapping("/api/user/public/register")
    public void register(@Valid @RequestBody RegisterFormDTO requestParam) {
        userService.register(requestParam);
    }

    /**
     * 用户登录
     *
     * @param requestParam 登录表单请求参数
     * @return 用户信息
     */
    @PostMapping("/api/user/public/login")
    public UserInfoVO login(@Valid @RequestBody LoginFormDTO requestParam) {
        return userService.login(requestParam);
    }

    /**
     * 获取使用的空间容量
     *
     * @return 结果
     */
    @GetMapping("/api/user/private/use-space")
    public UseSpaceVO getUseSpace() {
        Long userId = StpUtil.getLoginIdAsLong();
        return userService.getUseSpace(userId);
    }

    /**
     * 重置密码
     *
     * @param requestParam 重置密码表单请求参数
     */
    @PostMapping("/api/user/public/reset-password")
    public void resetPassword(@Valid @RequestBody ResetPwdFormDTO requestParam) {
        userService.resetPassword(requestParam);
    }

    /**
     * 退出登录
     */
    @PostMapping("/api/user/private/logout")
    public void logout() {
        StpUtil.logout();
    }

}
